/*
 * Copyright (c) 2009. Silenus Consultoria, S.L.
 */
package es.silenus.detecta.dao.impl;

import es.silenus.detecta.dao.KnowledgeUnitDAO;

import es.silenus.detecta.model.KnowledgeUnit;

import org.hibernate.FetchMode;
import org.hibernate.Session;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;

import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;

import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import static java.util.Arrays.asList;
import java.util.List;
import java.util.Map;


/**
 * DOCUMENT ME!
 *
 * @author <a href="mailto:dgarcia@silenus-consultoria.es">Daniel Garc&iacute;a</a>
 *
 * @since 02-abr-2009 12:38:12
 */
public class KnowledgeUnitDAOImpl extends ReadWriteDAOImpl<KnowledgeUnit, Long> implements KnowledgeUnitDAO {
	/**
	 * The check remove units SQL.
	 */
	private String checkRemoveUnitsSQL;

	/**
	 * Sets the check remove units SQL.
	 *
	 * @param checkRemoveUnitsSQL the check remove units SQL.
	 */
	public void setCheckRemoveUnitsSQL(String checkRemoveUnitsSQL) {
		this.checkRemoveUnitsSQL = checkRemoveUnitsSQL;
	}

	/**
	 * Finds a qualification given its code and organization id.
	 *
	 * @param code the code.
	 * @param organizationId the organization id.
	 *
	 * @return the area.
	 */
	@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
	public KnowledgeUnit findByCode(final String code, int organizationId) {
		Session session = null;

		try {
			session = getSession();

			return (KnowledgeUnit)session.getNamedQuery(KnowledgeUnit.class.getName() + ".findByCode").setString(
				"code", code).setInteger("organizationId", organizationId).uniqueResult();
		} finally {
			releaseSession(session);
		}
	}

	/**
	 * Override this method to provide a find by example page criteria.
	 *
	 * @param criteria the criteria.
	 * @param object the sample object.
	 *
	 * @return the criteria.
	 */
	protected DetachedCriteria preparePageCriteria(final DetachedCriteria criteria, final Map<String, Object> object) {
		criteria.setFetchMode("organization", FetchMode.JOIN);

		Object temp = object.get("id");

		if(temp != null) {
			criteria.add(Restrictions.eq("id", temp));
		}

		temp = object.get("code");

		if(temp != null) {
			criteria.add(Restrictions.ilike("code", (String)temp, MatchMode.ANYWHERE));
		}

		temp = object.get("name");

		if(temp != null) {
			criteria.add(Restrictions.ilike("name", (String)temp, MatchMode.ANYWHERE));
		}

		temp = object.get("description");

		if(temp != null) {
			criteria.add(Restrictions.ilike("description", (String)temp, MatchMode.ANYWHERE));
		}

		temp = object.get("organization.id");

		if(temp != null) {
			criteria.add(Restrictions.eq("organization.id", temp));
		}


		return criteria;
	}

	/**
	 * Retrieves the removal information of entities with the given keys.
	 *
	 * @param keys the keys.
	 *
	 * @return the information requested.
	 */
	@SuppressWarnings({"unchecked"})
	@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
	public List<Map<String, Object>> getRemovalInformation(final Long[] keys) {
		MapSqlParameterSource args = new MapSqlParameterSource();

		args.addValue("ids", asList(keys));

		return jdbcTemplate.queryForList(checkRemoveUnitsSQL, args);
	}

	/**
	 * Removes units by keys.
	 *
	 * @param keys the keys.
	 */
	@Transactional(propagation = Propagation.REQUIRED)
	public void remove(final Long[] keys) {
		List<KnowledgeUnit> elements = findByIds(keys);

		for(KnowledgeUnit element : elements) {
			super.remove(element);
		}
	}
}
